import { request } from '@/service/request';

/**
 * 查询作业列表
 */
export function fetchJobList(params?: Api.Dataplatform.JobSearchParams) {
  return request<Api.Dataplatform.JobList>({
    url: '/dataplatform/job/list',
    method: 'get',
    params
  });
}

/**
 * 查询所有作业
 */
export function fetchAllJobs(params?: Api.Dataplatform.JobSearchParams) {
  return request<Api.Common.CommonRecord<Api.Dataplatform.Job[]>>({
    url: '/dataplatform/job/listAll',
    method: 'get',
    params
  });
}

/**
 * 查询作业详情
 */
export function fetchJobDetail(id: number) {
  return request<Api.Common.CommonRecord<Api.Dataplatform.Job>>({
    url: `/dataplatform/job/${id}`,
    method: 'get'
  });
}

/**
 * 创建作业
 */
export function addJob(data: Api.Dataplatform.JobEdit) {
  return request<Api.Common.CommonRecord<number>>({
    url: '/dataplatform/job',
    method: 'post',
    data
  });
}

/**
 * 更新作业
 */
export function updateJob(data: Api.Dataplatform.JobEdit) {
  return request({
    url: '/dataplatform/job',
    method: 'put',
    data
  });
}

/**
 * 删除作业
 */
export function deleteJob(id: number) {
  return request({
    url: `/dataplatform/job/${id}`,
    method: 'delete'
  });
}

/**
 * 启动作业
 */
export function startJob(id: number, triggerType: string = 'MANUAL') {
  return request<Api.Common.CommonRecord<number>>({
    url: `/dataplatform/job/${id}/start`,
    method: 'post',
    params: { triggerType }
  });
}

/**
 * 停止作业实例
 */
export function stopJobInstance(instanceId: number) {
  return request({
    url: `/dataplatform/job/instance/${instanceId}/stop`,
    method: 'post'
  });
}

/**
 * 重跑作业实例
 */
export function rerunJobInstance(instanceId: number) {
  return request<Api.Common.CommonRecord<number>>({
    url: `/dataplatform/job/instance/${instanceId}/rerun`,
    method: 'post'
  });
}

/**
 * 查询作业实例列表
 */
export function fetchJobInstanceList(params?: Api.Dataplatform.JobInstanceSearchParams) {
  return request<Api.Dataplatform.JobInstanceList>({
    url: '/dataplatform/job/instance/list',
    method: 'get',
    params
  });
}

/**
 * 查询作业实例详情
 */
export function fetchJobInstanceDetail(instanceId: number) {
  return request<Api.Common.CommonRecord<Api.Dataplatform.JobInstance>>({
    url: `/dataplatform/job/instance/${instanceId}`,
    method: 'get'
  });
}

/**
 * 同步作业实例状态
 */
export function syncJobInstanceStatus(instanceId: number) {
  return request({
    url: `/dataplatform/job/instance/${instanceId}/sync`,
    method: 'post'
  });
}

/**
 * 获取作业实例日志
 */
export function fetchJobInstanceLogs(instanceId: number) {
  return request<Api.Common.CommonRecord<string>>({
    url: `/dataplatform/job/instance/${instanceId}/logs`,
    method: 'get'
  });
}
